package com.caoJun.jdbcTemplate;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Description: 转账功能的实现
 * @Author 83-start
 * @Time 2021/4/21 22:25
 */
public class TransferAccounts {
    public AccountDaoImpl getAccountDao() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountDaoImpl accountDao = context.getBean("accountDao", AccountDaoImpl.class);
        return accountDao;
    }

    public void TransferAccounts(int use1Id,int use2Id,double balance) {
        AccountDaoImpl accountDao = this.getAccountDao();
        // 查询转出用户的余额
        double balance1 = accountDao.selectBalanceById(use1Id);
        if (balance1 < balance){
            System.exit(0);
        }

        // 查询转入用户的余额
        double balance2 = accountDao.selectBalanceById(use2Id);

        // 转出操作
        accountDao.updateBalance(1,balance1 - balance);

        // 转入操作
        accountDao.updateBalance(2,balance2 + balance);
    }
}
